package com.todod.base.module.base.service.site;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.todod.base.framework.mybatis.core.query.QueryWrapperX;
import com.todod.base.module.base.enums.CommonConstants;
import com.todod.base.module.base.util.MD5Encrypt;
import com.todod.base.module.base.util.RemoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import com.todod.base.module.base.controller.admin.site.vo.*;
import com.todod.base.module.base.dal.dataobject.site.SiteDO;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.util.object.BeanUtils;

import com.todod.base.module.base.dal.mysql.site.SiteMapper;

import static com.todod.base.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.todod.base.module.base.enums.ErrorCodeConstants.*;

/**
 * 现场 Service 实现类
 *
 * @author 超管儿
 */
@Slf4j
@Service
@Validated
public class SiteServiceImpl implements SiteService {

    @Resource
    private SiteMapper siteMapper;
    @Resource
    private RemoteService remoteService;

    @Override
    public String createSite(SiteSaveReqVO createReqVO) {

        // 将系统输入井名转换成拼音+MD5加密处理 存给井编号
        createReqVO.setWellCode(MD5Encrypt.md5Encrypt(createReqVO.getWellName()));
        log.info("系统转换成拼音+MD5处理的井编号"+ createReqVO.getWellCode());

        // 检查井ID是否为空  来判断井来源
        if (StringUtils.isEmpty(createReqVO.getWellId())) {
            //井ID为空,井来源为O
            createReqVO.setWellSource("O");
        }else {
            //井ID不为空,井来源为E
            createReqVO.setWellSource("E");
        }
        // 插入
        SiteDO site = BeanUtils.toBean(createReqVO, SiteDO.class);
        site.setState(false);
        siteMapper.insert(site);

        remoteService.sendBaseInfoByCode(site.getWellCode(), CommonConstants.well);
        // 返回
        return site.getWellCode();
    }

    @Override
    public void updateSite(SiteSaveReqVO updateReqVO) {
        // 校验存在
        validateSiteExists(updateReqVO.getWellCode());
        // 更新
        /*// 检查wellId是否为空
        if (StringUtils.isEmpty(updateReqVO.getWellId())) {
            //根据系统时间生成日期时间字符串做ID
            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            String formattedDateTime = now.format(formatters);
            updateReqVO.setWellId(formattedDateTime);
            *//*
            // 生成唯一wellId，使用UUID保证唯一性
            String wellId = UUID.randomUUID().toString().replace("-", "");
            System.out.println("-------------------------生成随机井ID"+wellId);
            updateReqVO.setWellId(wellId);
            *//*
            //wellId为空,井来源为O
            updateReqVO.setWellSource("O");
        } else {
            //wellId为空,井来源为E
            updateReqVO.setWellSource("E");
        }
       */
        SiteDO updateObj = BeanUtils.toBean(updateReqVO, SiteDO.class);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("well_code", updateObj.getWellCode());
        siteMapper.update(updateObj,wrapper);

        remoteService.sendBaseInfoByCode(updateObj.getWellCode(), CommonConstants.well);
    }

    @Override
    public void deleteSite(String WellCode) {
        // 校验存在
        validateSiteExists(WellCode);
        // 删除
        siteMapper.deleteByWellCode(WellCode);

        remoteService.sendBaseInfoByCode(WellCode, CommonConstants.well);
    }


    /**
     * 启用
     */
    @Override
    public void enable(String wellCode) {
        log.info("调用启用仪器设备业务逻辑层");
        // 校验存在
        validateSiteExists(wellCode);
        siteMapper.enable(wellCode);

        remoteService.sendBaseInfoByCode(wellCode, CommonConstants.well);
    }
    /**
     * 禁用
     */
    @Override
    public void disable(String wellCode) {
        log.info("调用禁用仪器设备业务逻辑层");
        // 校验存在
        validateSiteExists(wellCode);
        siteMapper.disable(wellCode);

        remoteService.sendBaseInfoByCode(wellCode, CommonConstants.well);
    }
    @Override
    public SiteDO getSite(String wellCode) {
        return siteMapper.getByWellCode(wellCode);
    }


    @Override
    public PageResult<SiteDO> getSitePage(SitePageReqVO pageReqVO) {
        return siteMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SiteDO> getListSite(String params) {
        return siteMapper.getListSite(params);
    }




    private void validateSiteExists(String wellCode) {
        if (siteMapper.getByWellCode(wellCode) == null) {
            throw exception(SITE_NOT_EXISTS);
        }
    }

    /**
     * 获取现场已经启用的井数量
     */
    public int getWellNum(){
        return siteMapper.getWellNum();
    }

}